package com.zy.cat.controller.ad.v1;

import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.entity.dev.resp.CatDevWelfareInfoResp;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.service.CatAdService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @info
 * @author Link
 * @date 2019-11-12 16:31
 */
@Api(value = "cat_app_ad_info", tags = { "9趣猫APP广告信息统计开放API" }, description = "9趣猫APP广告统计开放接口")
@RestController
@RequestMapping("/v1/app/ad")
public class CatADController {

	@Autowired
	CatAdService catAdService;
	
	@PostMapping("/oper")
	@ApiOperation(position = 1, value = "统计广告展示点击击量", notes = "", response = RSPE.class, responseContainer = "Map", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "登录返回字段", example = "",required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "oper", value = "统计类型(0.展示、1.点击)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "ftype", value = "翻倍广告(0.否、1.是)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "ltype", value = "广告来源平台(0.穿山甲、1.腾讯)", example = "0", required = false, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "brand", value = "手机品牌", example = "Honer", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "model", value = "品牌型号", example = "HSF-001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imei", value = "手机IMEI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imsi", value = "手机IMSI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_oper_uuid_rtype_vscode_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_oper_uuid_rtype_vscode", MD5 = "token")
	@CheckDevPermission
	public Object setAppCPA(String pid,Integer rtype,@RequestParam(required = false,defaultValue = "0") Integer ltype,Integer oper,Integer ftype, String brand, String model, String vscode, String cid, String imei,
			String imsi, String uuid, String utoken, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (oper == null || (oper != 0 && oper != 1)) {
			return new RSPE<Object>(-2, "统计类型参数错误！");
		}
		if (ftype == null || (ftype != 0 && ftype != 1)) {
			return new RSPE<Object>(-2, "翻倍广告类型参数错误！");
		}
		if (ltype == null || (ltype != 0 && ltype != 1)) {
			return new RSPE<Object>(-2, "广告来源参数错误！");
		}
		catAdService.insertAdOper(pid,rtype,oper,ftype,ltype, brand, model, vscode, cid, imei, imsi);
		return new RSPE<Object>(1, "success");
	}
	
	@PostMapping("/switch")
	@ApiOperation(position = 1, value = "获取广告开关状态", notes = "获取广告开关状态(0.关闭、1.打开)", response = RSPE.class, responseContainer = "Map", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "brand", value = "手机品牌", example = "Honer", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "model", value = "品牌型号", example = "HSF-001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "appId", value = "应用ID(0.九趣猫(默认)、1.淘淘集、2.一陶)", example = "0", required = false, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "imei", value = "手机IMEI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imsi", value = "手机IMSI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_uuid_rtype_vscode_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_uuid_rtype_vscode", MD5 = "token")
	public Object getAdSwitch(Integer rtype,@RequestParam(required = false,defaultValue = "0") Integer appId,String brand, String model, String vscode, String cid, String imei,
			String imsi, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}

		Callable<RSPE<Integer>> result = new Callable<RSPE<Integer>>() {
			@Override
			public RSPE<Integer> call() throws Exception {
				Integer resp=catAdService.getAdSwitch(rtype, brand, model, vscode, cid, imei, imsi);
				if(resp!=null) {
					return new RSPE<Integer>(resp);
				}
				return new RSPE<Integer>(-1,"获取失败！");
			}
		};
		return result;
	}
}
